<template>
  <div class="main">
    <div class="route-wrapper">
      <span class="go-history" @click="goHistory()">{{
        $route.params.history
      }}</span>
      <span>>{{ $route.params.result.title }}</span>
    </div>
    <div class="detail-wrapper">
      <div class="introduction-wrapper">
        <div class="introduction">
          <div class="intro-content">
            <div class="header-content">
              <div class="left-content">
                <div class="product-title">
                  {{ $route.params.result.title }}
                </div>
                <div class="product-detail">
                  <span class="product-company">{{
                    $route.params.result.company
                  }}</span>
                  <span class="product-date"
                    >发布于：{{ $route.params.result.date }}</span
                  >
                </div>
              </div>
              <div class="btn-box">
                <button v-if="this.isAlgo()" class="btn">立即咨询</button>
                <button v-else class="btn" @click="bid">投标</button>
              </div>
            </div>
            <div class="intro-content">
              {{ $route.params.result.content }}
            </div>
          </div>
        </div>
        <div v-if="this.isAlgo()">
          <div class="info-nav-wrapper" v-if="this.isAlgo()">
            <div class="info-nav" v-if="this.detailsList">
              <div
                class="nav-item"
                v-for="(detail, index) in this.detailsList"
                :key="detail"
                @click="navScroll(index)"
              >
                {{ detail }}
              </div>
              <div class="nav-scroll" :style="{}"></div>
            </div>
          </div>
          <div class="detail-content-wrapper" v-if="this.isAlgo()">
            <div class="detail-content-box">
              <div class="detail-box">
                <div class="part" v-if="this.isAlgo()">
                  <div class="liked" @click="fabulous()">
                    <i class="fa fa-thumbs-up good"></i>
                    {{ liked }}
                  </div>
                </div>
              </div>
              <div class="detail-box">
                <div class="part"></div>
              </div>
            </div>
          </div>
        </div>
        <div v-else>
          <div class="demand-info info-box">
            <div class="demand-title info-box-title">需求内容</div>
            <div class="demand-content">
              <div class="item-box">
                <div class="item-box-name">开发周期</div>
                <div class="item-box-content">
                  {{ $route.params.result.period }}
                </div>
              </div>
              <div class="item-box">
                <div class="item-box-name">硬件要求</div>
                <div class="item-box-content">
                  {{ $route.params.result.requires }}
                </div>
              </div>
              <div class="item-box">
                <div class="item-box-name">系统和软件版本-</div>
                <div class="item-box-content">
                  {{ $route.params.result.version }}
                </div>
              </div>
              <div class="item-box">
                <div class="item-box-name">达标要求</div>
                <div class="item-box-content">
                  {{ $route.params.result.standard }}
                </div>
              </div>
            </div>
          </div>
          <div class="employer-info info-box">
            <div class="employer-title info-box-title">雇主信息</div>
            <div class="employer-content">
              <div class="item-box">
                <div class="item-box-name">地区</div>
                <div class="item-box-content">
                  {{ $route.params.result.area }}
                </div>
              </div>
              <div class="item-box">
                <div class="item-box-name">联系人</div>
                <div class="item-box-content">
                  {{ $route.params.result.contact }}
                </div>
              </div>
              <div class="item-box">
                <div class="item-box-name">电话号码</div>
                <div class="item-box-content">
                  {{ $route.params.result.phone }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="warn-wrapper">
      <div class="little-point"></div>
      <span class="warn-info">{{ this.warning }}</span>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "detail",
  data() {
    return {
      liked: this.$route.params.result.liked,
      detailsList: ["产品详情", "开发者文档"],
      warning: "请登录后再操作",
    };
  },
  methods: {
    // 实现导航栏滚动效果
    navScroll(index) {
      let navScroll = document.getElementsByClassName("nav-scroll")[0];
      let contentScroll =
        document.getElementsByClassName("detail-content-box")[0];
      navScroll.style.left = index * 104 + "px";
      contentScroll.style.left = -(index * contentScroll.offsetWidth) + "px";
    },
    // 判断是否为算法广场
    isAlgo() {
      // 通过路径判断
      let algo = new RegExp("algorithm");
      if (algo.test(document.location)) {
        return true;
      } else {
        return false;
      }
    },
    // 前往上一个页面
    goHistory() {
      if (this.isAlgo()) {
        this.$router.push({
          //和router-link标签中的跳转对象相同
          name: "algorithm",
          params: {
            userData: this.$route.params.userData,
          },
        });
      } else {
        this.$router.push({
          //和router-link标签中的跳转对象相同
          name: "demand",
          params: {
            userData: this.$route.params.userData,
          },
        });
      }
    },
    // 点赞
    fabulous() {
      // 通过登录时传到路由中的userData判断是否已经登录
      if (!this.$route.params.userData || this.$route.params.userData == "") {
        this.warning = "请登录后再进行操作";
        document.getElementsByClassName(
          "little-point"
        )[1].style.backgroundColor = "yellow";
        this.warn();
        return 0;
      } else {
        axios({
          method: "GET",
          url:
            "/api/user/like?modelId=" +
            this.$route.params.result.id +
            "&userId=" +
            this.$route.params.userData.id,
        }).then(
          () => {
            this.liked += 1;
            document.getElementsByClassName(
              "little-point"
            )[1].style.backgroundColor = "green";
            this.warning = "点赞成功";
            this.warn();
          },
          () => {
            document.getElementsByClassName(
              "little-point"
            )[1].style.backgroundColor = "yellow";
            this.warning = "请检查你的网络";
            this.warn();
          }
        );
      }
    },
    // 投标
    bid() {
      if (!this.$route.params.userData || this.$route.params.userData == "") {
        this.warning = "请登录后再进行操作";
        document.getElementsByClassName(
          "little-point"
        )[1].style.backgroundColor = "yellow";
        this.warn();
        return 0;
      } 
      else {
        axios({
          method: "GET",
          url:
            "/api/user/bid?requestId=" +
            this.$route.params.result.id +
            "&userId=" +
            this.$route.params.userData.id,
        }).then(
          () => {
            this.liked += 1;
            document.getElementsByClassName(
              "little-point"
            )[1].style.backgroundColor = "green";
            this.warning = "投标成功";
            this.warn();
          },
          () => {
            document.getElementsByClassName(
              "little-point"
            )[1].style.backgroundColor = "yellow";
            this.warning = "请检查你的网络";
            this.warn();
          }
        );
      }
    },
    // 提示框出现
    warn() {
      let warnInfo = document.getElementsByClassName("warn-wrapper")[1];
      warnInfo.style.right = 220 + "px";
      let t = setTimeout(function () {
        warnInfo.style.right = -250 + "px";
        clearTimeout(t);
      }, 5000);
    },
  },
};
</script>

<style>
.main {
  position: relative;
}
.route-wrapper {
  width: 67%;
  font-size: 16px;
  text-align: left;
  margin-top: 2%;
}
.go-history {
  cursor: pointer;
  font-weight: 600;
}
.detail-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  width: 67%;
  margin-top: 2%;
  margin-bottom: 2%;
  color: #000;
}
.introduction-wrapper {
  width: 73%;
  padding: 25px;
  background: #fff;
  border-radius: 20px;
}
.introduction {
  margin-bottom: 5%;
}
.header-content {
  display: flex;
  justify-content: space-between;
  margin-bottom: 4%;
}
.product-title {
  font-size: 22px;
  margin-bottom: 4%;
}
.product-detail {
  font-size: 12px;
  color: #969bab;
}
.product-company {
  margin-right: 15px;
}
.btn-box {
  font-size: 12px;
}
.btn {
  cursor: pointer;
  width: 75px;
  height: 30px;
  border-radius: 5px;
  color: #fff;
  background-color: #8c30f5;
}
.intro-content {
  font-size: 15px;
  margin-bottom: 4%;
}
.detail-content-wrapper {
  overflow: hidden;
  widows: 100%;
}
.detail-content-box {
  display: flex;
  flex-direction: row;
  position: relative;
  left: 0;
  will-change: left;
  transition: left 0.3s ease-in-out;
}
.detail-box {
  flex-shrink: 0;
  width: 100%;
  overflow: hidden;
}
.part {
  padding: 2.75% 0;
  display: flex;
  align-items: center;
  flex-direction: column;
  width: 100%;
}
.liked {
  cursor: pointer;
  border: 1px solid #8c30f5;
  width: 65px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.good {
  display: inline-block;

  font-style: normal;
  font-weight: 400;
  line-height: 1;
  vertical-align: -0.125em;
  text-align: center;
}
.info-box {
  text-align: left;
}
.demand-info {
  margin-bottom: 6%;
}
.info-box-title {
  font-size: 22px;
  margin-bottom: 3%;
}
.item-box {
  font-size: 15px;
  margin-bottom: 2%;
}
.item-box-name {
  display: inline-block;
  color: #969bab;
  width: 22%;
  margin-right: 3%;
}
.item-box-content {
  display: inline-block;
  width: 75%;
}
</style>

